import { getFuelRecords } from './fuel.js';
import { getExpenseRecords } from './expense.js';

/**
 * 获取加油统计数据
 * 
 * @param {string} carId - 车辆ID
 * @param {string} [year='全部'] - 年份，可选
 * @returns {Promise<Object>}
 */
export const getFuelData = async (carId, year = '全部') => {
  try {
    const [fuelResult, expenseResult] = await Promise.all([
      getFuelRecords(carId, year),
      getExpenseRecords(carId, year)
    ]);

    return {
      ...fuelResult.fuelStatistics,
      expenseTotalCosts: expenseResult.expenseTotalCosts
    };
  } catch (error) {
    console.error('获取加油统计数据失败:', error);
    return {};
  }
};

/**
 * 获取油耗趋势数据
 * 
 * @param {string} carId - 车辆ID
 * @param {string} [year='全部'] - 年份，可选
 * @returns {Promise<Array>}
 */
export const getTrendData = async (carId, year = '全部') => {
  try {
    const fuelResult = await getFuelRecords(carId, year);
    return fuelResult.fuelRecords.map(record => ({
      date: record.date,
      fuelEconomy: record.fuelEconomy
    }));
  } catch (error) {
    console.error('获取油耗趋势数据失败:', error);
    return [];
  }
};

/**
 * 获取养车记录数据
 * 
 * @param {string} carId - 车辆ID
 * @param {string} [year='全部'] - 年份，可选
 * @returns {Promise<Array>}
 */
export const getExpenseData = async (carId, year = '全部') => {
  try {
    const expenseResult = await getExpenseRecords(carId, year);
    return expenseResult.expenseTypeStatistics;
  } catch (error) {
    console.error('获取养车记录数据失败:', error);
    return [];
  }
};
